/**
 * Created by three on 14-11-4.
 */
'use strict';

/**
 * 日志服务
 */
angular.module('th.logger', [])
    .provider('logger', function () {
        this.props = {
            level: 'info',
            output: 'browser',
            format: 'Location[{type}]: at ({function} {path}{file}:{row}:{column}) => {msg}'
        };
        this.config = function (opts) {
        };
        this.$get = function () {
            var logger = {
                _print: function () {
                    for (var i = 0; i < arguments.length; i++) {
                        console.log(arguments[i]);
                    }
                    return logger;
                },
                log: function () { // 支持输出对象
                    try {
                        throw new Error();
                    } catch (e) {
                        var loc = e.stack.replace(/Error\n/).split(/\n/)[1].replace(/^\s+|\s+$/, "");
                        // 一般输出
                        if (arguments.length == 1) {
                            loc = "Location[log]: " + loc + " => "/*.replace(/%c/g,"")*/;
                            logger._print(loc, arguments[0]);
                        } else if (arguments.length == 2) {
                            // 输出异常
                            loc = "Location[log]: " + loc + " => ";
                            logger._print(loc, arguments[0], "Exception Stack:" + arguments[1].stack);
                        }
                    }
                    return logger;
                },
                info: function () {
                    try {
                        throw new Error();
                    } catch (e) {
                        var loc = e.stack.replace(/Error\n/).split(/\n/)[1].replace(/^\s+|\s+$/, "");
                        // 一般输出
                        if (arguments.length == 1) {
                            loc = "Location[info]: " + loc + " => "/*.replace(/%c/g,"")*/;
                            logger._print(loc + arguments[0]);
                        } else if (arguments.length == 2) {
                            // 输出异常
                            loc = "Location[info]: " + loc + " => ";
                            logger._print(loc + arguments[0], "Exception Stack:" + arguments[1].stack);
                        }
                    }
                    return logger;
                }
            };
            logger.debug = logger.error = logger.warn = logger.info;
            return logger;
        }
    })
;